<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */



class ChangePassword extends HTML_QuickForm {
  private $tpl;
  private $user;
  
  function ChangePassword($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;
    parent::HTML_QuickForm('changepassword','post','','','onsubmit="javascript:password1.value=hex_sha1(password1.value), password2.value=hex_sha1(password2.value)"');
    $this->addElement('header', 'header', 'changepassword');
    $this->addElement('password', 'password1', _('password'), array('size' => 30, 'maxlength' => 50, 'value' => uniqid()));
    $this->addElement('password', 'password2', _('repeat password'), array('size' => 30, 'maxlength' => 50, 'value' => uniqid()));
    $this->addElement('submit', 'btn_Submit', _('Submit'), array('class' => 'submit','onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#000000\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('compare','function','compare_field', $this); 
    $this->addRule('password1', _('password required'), 'required');
    $this->addRule('password2', _('password required'), 'required');
    $this->addRule('password1', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password2', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password1', _('passwords do not match'), 'compare', 'password2');
  }
    
  function compare_field($element, $value) {
    if ($element == $this->getElementValue($value)) {
      return true;
    } else {
      return false;
    }
  } 
   
  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }
  
  function process() {
    $this->user->setPassword($this->getSubmitValue('password1'));
    $this->user->change_password();
    $this->tpl->assign('result', true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }
   
} //end of class

?>